package dao

import (
	"myapp/common"
	"myapp/model"
	"myapp/utils"
	"net/http"
	"strconv"

	"github.com/gin-gonic/gin"
)

func Cargo_Com(c *gin.Context) {
	db := common.InitDB()
	var cargoCompanies []*model.CargoCompany
	err := db.Find(&cargoCompanies).Error
	if err != nil {
		c.JSON(http.StatusNotFound, utils.Result{
			Code:    utils.ERROR,
			Message: "查询失败！",
		})
	} else {
		c.JSON(http.StatusOK, utils.Result{
			Code: utils.SUCCESS,
			Data: gin.H{
				"cargoCompanies": cargoCompanies,
			},
		})
	}
}

func Get_cargo_Com(c *gin.Context) {
	db := common.InitDB()
	pagenum, _ := strconv.Atoi(c.PostForm("pagenum"))
	pagesize, _ := strconv.Atoi(c.PostForm("pagesize"))
	//query := c.PostForm("query")

	var count int64
	var cargoCompanies []*model.CargoCompany

	err := db.Order("id").Limit(pagesize).Offset((pagenum - 1) * pagesize).Find(&cargoCompanies).Error
	if err != nil {
		c.JSON(http.StatusNotFound, utils.Result{
			Code:    utils.ERROR,
			Message: "查询失败！",
		})
	}
	err = db.Table("cargo_companies").Count(&count).Error
	if err != nil {
		c.JSON(http.StatusNotFound, utils.Result{
			Code:    utils.ERROR,
			Message: "查询失败！",
		})
	} else {
		c.JSON(http.StatusOK, utils.Result{
			Code: utils.SUCCESS,
			Data: gin.H{
				"cargoCompanies": cargoCompanies,
				"total":          count,
			},
		})
	}
}
func Get_id_cargoCom(c *gin.Context) {
	db := common.InitDB()
	id := c.Query("id")

	var cargoCompany *model.CargoCompany
	err := db.Where("id = ?", id).Find(&cargoCompany).Error
	if err != nil {
		c.JSON(http.StatusNotFound, utils.Result{
			Code:    utils.ERROR,
			Message: "查询失败！",
		})
	} else {
		c.JSON(http.StatusOK, utils.Result{
			Code: utils.SUCCESS,
			Data: gin.H{
				"cargoCompany": cargoCompany,
			},
		})
	}
}
func Del_cargo_com(c *gin.Context) {
	db := common.InitDB()
	id := c.PostForm("id")
	err := db.Where("id = ?", id).Delete(&model.CargoCompany{}).Error
	if err != nil {
		c.JSON(http.StatusNotFound, utils.Result{
			Code:    utils.ERROR,
			Message: "删除失败！",
		})
	} else {
		c.JSON(http.StatusOK, utils.Result{
			Code:    utils.SUCCESS,
			Message: "删除成功！",
		})
	}
}

func Add_cargo_com(c *gin.Context) {
	db := common.InitDB()
	name := c.PostForm("name")
	cargoCompany := model.CargoCompany{Name: name}

	err := db.Create(&cargoCompany).Error
	if err != nil {
		c.JSON(http.StatusNotFound, utils.Result{
			Code:    utils.ERROR,
			Message: "创建失败！",
		})
	} else {
		c.JSON(http.StatusOK, utils.Result{
			Code:    utils.SUCCESS,
			Message: "创建成功！",
		})
	}
}
func Modify_cargo_com(c *gin.Context) {
	db := common.InitDB()
	id, _ := strconv.ParseInt(c.PostForm("id"), 10, 64)
	name := c.PostForm("name")
	cargoCompany := model.CargoCompany{ID: uint(id), Name: name}

	err := db.Model(&cargoCompany).Where("id = ?", id).Updates(cargoCompany).Error

	if err != nil {
		c.JSON(http.StatusNotFound, utils.Result{
			Code:    utils.ERROR,
			Message: "修改失败！",
		})
	} else {
		c.JSON(http.StatusOK, utils.Result{
			Code:    utils.SUCCESS,
			Message: "修改成功！",
		})
	}
}

func Index(c *gin.Context) {

	c.JSON(http.StatusOK, utils.Result{
		Code:    utils.SUCCESS,
		Message: "welcome",
	})
}





